﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using Kobets.Automation.Chimera.Common;
using Kobets.Automation.Chimera.Orchestrator.ControllerService;
using Kobets.Automation.Chimera.Orchestrator.Models;

namespace Kobets.Automation.Chimera.Orchestrator.Shared
{
    /// <summary>
    /// The whole point of this helper class is to communicate to controller
    /// </summary>
    public static class Common
    {
        private static readonly ControllerContractClient cf = new ControllerContractClient();

        public static Agent[] GetAgentsList()
        {
            if (cf.ChannelFactory.State != CommunicationState.Opened) cf.Open();
            return cf.GetAssociatedAgents();
        }

        public static void SetAgentGroup(string agent, string @group)
        {
            if (agent == null) throw new ArgumentNullException("agent");

            if (cf.ChannelFactory.State != CommunicationState.Opened) cf.Open();
            cf.SetAgentGroup(agent, @group);
        }

        public static void ExecuteRun(Models.TestPackages testPackage, Enums.DistributionStyle distributionStyle, string group, TestCase[] testCases, string user, bool enableSLM)
        {
            if (testPackage == null) throw new ArgumentNullException("testPackage");
            if (@group == null) throw new ArgumentNullException("group");
            if (testCases == null) throw new ArgumentNullException("testCases");
            if (user == null) throw new ArgumentNullException("user");

            var workload = new WorkLoad
                               {
                                   ExecutionPath = testPackage.ExecutionPath,
                                   PathToSln = testPackage.PathToSolution,
                                   SlnName = testPackage.SolutionName,
                                   TfsProjectUri = new Uri(testPackage.TFSProjectUri),
                                   UseTfs = testPackage.UseTFS,
                                   DistributionStyle = distributionStyle,
                                   Group = @group,
                                   TestCases = testCases,
                                   User = user,
                                   EnableSlm = enableSLM
                               };

            cf.ExecuteWorkload(workload);
        }

        public static void StopExecution(string @group)
        {
            cf.StopExecution(@group);
        }

        public static void AddAgent(string agentName, string @group, Dictionary<string, string> attributes)
        {
            if (agentName == null) throw new ArgumentNullException("agentName");

            if (cf.ChannelFactory.State != CommunicationState.Opened) cf.Open();
            cf.RegisterAgent(new Agent{ Name = agentName, NetworkName = agentName, Controller = cf.Endpoint.Address.Uri.Host, Group = @group, Attributes = attributes });
        }

        public static void RemoveAgent(string agentName)
        {
            if (agentName == null) throw new ArgumentNullException("agentName");

            if (cf.ChannelFactory.State != CommunicationState.Opened) cf.Open();
            cf.UnregisterAgent(new Agent { Name = agentName });
        }

        public static void SetAttributes(string agentName, Dictionary<string, string> attributes)
        {
            if (agentName == null) throw new ArgumentNullException("agentName");

            if (cf.ChannelFactory.State != CommunicationState.Opened) cf.Open();
            cf.SetAgentAttributes(agentName, attributes);
        }
    }
}